<?php
include 'includes/login_check.php';
//Gets the id of the challenge to view, if not there, redirect to home page
if (isset($_GET['challenge_id'])) {
	$challenge_id = $_GET['challenge_id'];
} else {
	header("location: $home");
}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<?php include 'includes/head.php' ?>
	<link href="css/custom.css" rel="stylesheet" type="text/css" />
	<script language="javascript" src="js/battle.js"></script>
	<link rel="stylesheet" type="text/css" href="lightbox/lightbox.css" media="screen" />  
	<link rel="stylesheet" type="text/css" href="comment/css/style.css"  />

	<script type="text/javascript" src="lightbox/jquery.lightbox-0.5.js"></script>
	<script type="text/javascript">
		var challenge_id_var = <?php echo $challenge_id; ?>;
	</script>
	<script type="text/javascript" src="comment/js/comments.actions.js"></script>

	<script type="text/javascript">
		$(function() {
			$('.battle_item a').lightBox();
			$('.main-content').jqTransform({imgPath:'jqforms/img/'});
		});
	</script>
    
	<script src="js/infieldlabel.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript" charset="utf-8">
		$(function(){ $("label").inFieldLabels(); });
	</script>
</head>

<body class="view_challenge">

<div id="top">
    <div id="header">
        <?php include 'includes/nav_container.php' ?>
    </div>
</div>

<div id="wrapper">
<table><tr>

<td id="col-left">
    <?php include 'includes/current_activity.php'; ?>
</td>

<td id="col-main">
    <?php
    //Get contest info 
    $sqlquery = mysql_query("SELECT * FROM challenges WHERE id = '$challenge_id'");
    if (!$result) {
        header("location: $home");
    }
    while ($row = mysql_fetch_assoc($sqlquery)) {
        $title = $row['title'];
        $description = $row['description'];
        $win_by_votes = $row['win_by_votes'];
        $win_by_time = $row['win_by_times'];
        $created_on = $row['created_on'];
        $end_time = $row['ended_on'];
        if ($row['private'] == '0') {
            $private = 'private';
        } else {
            $private = 'public';
        }
    }
    ?>

    <div>
        <div class="main-title">
            <h2>
                <span><?php echo $title ?></span>
                <br class="cleft" />
            </h2>
        </div>
        <div class="main-content">
            <div class="block-left battlepage_container">
                <div class="bc_border">
                    <div class="bc_all">
                        <div class="bc_wrapper">
                            <form class="vote_form" name="" id="" method="POST" action="vote_process.php">
                                <input type="hidden" name="challenge_id"  value="<?php echo $challenge_id ?>" /> 
                                <ul>
									<?php
//get the contestants associated with the challenge
$sqlquery = sprintf("SELECT * FROM contestants WHERE contestant_challenge_id = '$challenge_id' ORDER BY votes");
$result = mysql_query($sqlquery) or die("no query");
$result_array = array();
while ($row = mysql_fetch_assoc($result)) {
$result_array[] = $row;
}
foreach ($result_array as $row) {
	$user_id = $row['contestant_user_id'];
	$contestant_username_query = mysql_query("SELECT username, oauth_uid FROM users WHERE id = '$user_id'");
	while ($row_getcontestantuser = mysql_fetch_assoc($contestant_username_query)) {
		$contestant_username = $row_getcontestantuser['username'];
		$contestant_oauth = $row_getcontestantuser['oauth_uid'];
}
$votes = $row['votes'];
$contestant_id = $row['id'];
$filepath = $row['image'];
$cont_description = $row['description'];
?>
									<script type="text/javascript">
										$(function(){
											$("#popup").hide();
											$(".u-pop").mousemove(function(e){
												var id=$(this).attr("id");
												var name=$(this).attr("name");
												$("#popup").html("<div><img class='pop_pi' src='https://graph.facebook.com/"+id+"/picture?type=large' /><p><span>"+name+"</span></p></div>");
												$("#popup").show();
												$("#popup").css({
													top: (e.pageY + -40) + "px",
													left: (e.pageX + 80) + "px"
												});
											});
											$(".u-pop").mouseout(function(e){
												$("#popup").hide();
											});
										});
									</script>
                                    <li class="battle_item">
                                        <div>
                                            <input type="radio" name="vote" value="<?php echo $contestant_id ?>"/>
                                            <a href="<?php echo $filepath ?>" title="<?php echo $cont_description ?>" class="preview"></a>
                                            <p class="u-pop" id="<?php echo $contestant_oauth; ?>" name="<?php echo $contestant_username ?>"><span><?php echo $votes ?>
                                               </span></p>
                                            <img src="<?php echo $filepath ?>" />
                                        </div>
                                    </li>
									<?php } ?>
									<li><br class="cleft" /></li>
								</ul>
                                <div class="submit">
                                    <input type="submit" value="">
                                </div>
                            </form>
                        </div>
                        <div class="bc_desc">
                            <p><span>Description</span><?php echo $description ?></p>
                        </div>
                        <div id="chl_comments">
                            <form id="newCommentForm">
                                <div class="add_comment">
                                    <label for="newComment">Add a Comment</label>
                                    <input type="text" id="newComment" name="newComment"/>
                                </div>
                                <div class="hidden"><button id="submitComment">Add Comment</button></div>
                                <input type="hidden" name="challenge_id" value="<?php echo $challenge_id ?>"/>
                                <input type="hidden" name="user_id" value="<?php 
									@session_start();
									$user = $_SESSION['id'];
									echo $user ?>"/>
                            </form>
                            <table id="displayComment" class="gridtable commentTable"></table>
                        </div>
                    </div>
                </div>
            </div>
            <div class="block-right">
                <div>
                    <h1>Challenge Info</h1>
                    <div class="challenge_info">
                        <?php
                        if (isset($win_by_votes) && isset($win_by_time) && $win_by_time != 0 && $win_by_votes != 0) {
                            echo "
    <div>Votes to win: <span>$win_by_votes</span></div>
    <div>End date: <span>$end_time</span></div>";
                        } elseif (isset($win_by_votes) && (!isset($win_by_time) || $win_by_time == 0) && $win_by_votes != 0) {
                            echo "
    <div>Votes to win: <span>$win_by_votes</span></div>";
                        } else {
                            echo "
    <div>End date: <span>$end_time</span></div>";
                        }
                        ?>
                    </div>

					<p><span>Public/Private: This contest is <?php echo $private ?></span></p><br />
						<?php //See if contest is full  ?>
                            <a href="join_contest.php?challenge_id=<?php echo $challenge_id ?>">Join Contest</a>
                            <ul>
                            <?php
                            //List of contestants - votes / ordered by votes
                            function array_sort_by_column(&$arr, $col, $dir = SORT_DESC) {
                                $sort_col = array();
                                foreach ($arr as $key => $row) {
                                    $sort_col[$key] = $row[$col];
                                }
                                array_multisort($sort_col, $dir, $arr);
                            }
                            array_sort_by_column($result_array, 'votes');
                            foreach ($result_array as $row) {
                                $user_id = $row['contestant_user_id'];
                                //Get the user
                                $sqlquery_user = mysql_query("SELECT username FROM users WHERE  id = '$user_id'");
                                while ($row_getuser = mysql_fetch_assoc($sqlquery_user)) {
                                    $user_name = $row_getuser['username'];
                                }
                                $votes = $row['votes'];
                            ?>
                            <li>
                                <?php echo $user_name ?> - <?php echo $votes ?>
                            </li>
                        <?php } ?>
                    </ul>
                </div>
            </div>
        </div>
    </td>
    <!--<td id="col-right"></td>-->
</tr></table>    

</div>

<div id="bottom">
</div>

<div id="popup" class="popup_container" style="position:absolute;" >
</div>

</body>

</html>
